package com.asp.alumnisystem.view;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.asp.alumnisystem.R;
import com.asp.alumnisystem.base.IRefreshView;
import com.asp.alumnisystem.view.SwipeRefreshView.SwipeRefresh;
import com.asp.alumnisystem.view.SwipeRefreshView.SwipeRefreshLayout;


/**
 * 下拉刷新,上拉加载更多.
 */
public class CustomRecyclerView extends RelativeLayout implements View.OnClickListener, IRefreshView {

    private Context context;
    private RecyclerView recycleView;
    private SwipeRefreshLayout refreshLayout;
    private LinearLayout errorLayout;
    private LinearLayout emptyLayout;
    private TitleBar titleBar;

    private RecyclerListener listener;

    public CustomRecyclerView(Context context) {
        this(context, null, 0);
    }

    public CustomRecyclerView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CustomRecyclerView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        this.context = context;
        initView();
    }

    private void initView() {
        LayoutInflater.from(context).inflate(R.layout.custom_recycler_view, this, true);
        refreshLayout = findViewById(R.id.swipe_refresh);
        recycleView = findViewById(R.id.recycler_view);
        recycleView.setLayoutManager(new LinearLayoutManager(context));
        refreshLayout.setColorSchemeResources(R.color.colorStartBlue, R.color.colorEndBlue);
        refreshLayout.setOnRefreshListener(new SwipeRefresh.OnRefreshListener() {
            @Override
            public void onRefresh() {
                if (listener != null){
                    listener.onRefresh(recycleView);
                }
            }
        });
        refreshLayout.setOnPullUpRefreshListener(new SwipeRefreshLayout.OnPullUpRefreshListener() {
            @Override
            public void onPullUpRefresh() {
                if (listener != null){
                    listener.loadMore(recycleView);
                }
            }
        });

        setBothMode();

        //监听布局显示出来后才显示刷新
        refreshLayout.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
            @Override
            public boolean onPreDraw() {
                refreshLayout.getViewTreeObserver().removeOnPreDrawListener(this);
                if (refreshLayout.canExecuteDownRefresh()){
                    startRefreshing();
                }
                return false;
            } });
    }

    public TitleBar getTitleBar(){
        if(titleBar == null){
            titleBar = findViewById(R.id.title_bar);
            titleBar.setVisibility(VISIBLE);
        }
        return titleBar;
    }

    public void setLayoutManager(RecyclerView.LayoutManager layoutManager){
        recycleView.setLayoutManager(layoutManager);
    }

    /**
     * 设置监听
     */
    public void setListener(RecyclerListener listener){
        this.listener = listener;
    }

    /**
     * 设置adapter
     */
    public void setAdapter(RecyclerView.Adapter adapter){
        recycleView.setAdapter(adapter);
    }

    /**
     * 设置可以上拉下拉模式
     */
    public void setBothMode(){
        setMode(SwipeRefresh.Mode.BOTH);
    }

    /**
     * 设置只能下拉模式
     */
    public void setRefreshMode(){
        setMode(SwipeRefresh.Mode.PULL_FROM_START);
    }

    /**
     * 自定义设置模式
     */
    public void setMode(SwipeRefresh.Mode mode){
        refreshLayout.setMode(mode);
    }

    /**
     * 开始刷新
     */
    public void startRefreshing(){
        showRefreshLayout();
        refreshLayout.setRefreshing(true);
    }

    /**
     * 停止刷新
     */
    public void stopRefreshing(){
        refreshLayout.stopRefreshAndPull();
    }

    /**
     * 显示没有内容时的提示界面
     */
    public void showEmptyLayout(){
        if (errorLayout != null){
            errorLayout.setVisibility(GONE);
        }
        refreshLayout.setVisibility(GONE);
        if (emptyLayout == null){
            emptyLayout = findViewById(R.id.empty_result);
            emptyLayout.setOnClickListener(this);
        }
        emptyLayout.setVisibility(VISIBLE);
    }

    /**
     * 显示网络中断时的提示界面
     */
    public void showNetErrorLayout(){
        if (emptyLayout != null){
            emptyLayout.setVisibility(GONE);
        }
        refreshLayout.setVisibility(GONE);
        if (errorLayout == null){
            errorLayout = findViewById(R.id.net_error_result);
            errorLayout.setOnClickListener(this);
        }
        errorLayout.setVisibility(VISIBLE);
    }

    /**
     * 显示正常的刷新界面
     */
    public void showRefreshLayout(){
        refreshLayout.setVisibility(VISIBLE);
        if (errorLayout != null){
            errorLayout.setVisibility(GONE);
        }
        if (emptyLayout != null){
            emptyLayout.setVisibility(GONE);
        }
    }

    @Override
    public void onClick(View v) {
        startRefreshing();
        if (listener != null){
            listener.onRefresh(recycleView);
        }
    }


    public interface RecyclerListener {
        /**
         * 刷新
         */
        void onRefresh(RecyclerView recyclerView);

        /**
         * 加载更多
         */
        void loadMore(RecyclerView recyclerView);
    }
}